/*
 * 本类存储所有数据表单
 * 2011-11-07:completed initially by gongshw
 */

package nju.mscs.data.object;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import nju.mscs.data.DataServerManager;

public class Database implements Serializable{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -4456996804844824504L;
	private Inventory inventory;
	private VipList vipList;
	private PromotionList promotionList;
	private UserList userList;
	private VipUpdataList vipUpdataList;
	private GiftList giftList;
	private SaleRecord saleRecord;
	private String shopId;
	
	public Database(Inventory i,VipList v,PromotionList p,UserList u,VipUpdataList vu,GiftList gl,SaleRecord sr){
		inventory = i;
		vipList = v;
		promotionList = p;
		userList = u;
		vipUpdataList = vu;
		giftList = gl;
		saleRecord = sr;
	}
	//--------------------------------------------------------------商品
	public Iterator<Goods> getInventory() {
		return inventory.getIterator();
	}
	
	public Goods searchGoodsById(String id){
		return inventory.search(id);
	}
	
	public boolean addGoods(String name,double price,String id,int amount){
		return inventory.add(new Goods(name,price,id,amount));
	}
	
	public boolean deleteGoods(String id){
		return inventory.delete(id);
	}
	//------------------------------------------------------------------vip

	public Iterator<VipConsumer> getVipList() {
		return vipList.getIterator();
	}
	
	public VipConsumer searchVipConsumerById(String id){
		return vipList.search(id);
	}
	public VipConsumer searchVipConsumerByTelephone(int tel){
		Iterator<VipConsumer> it = vipList.getIterator();
		if(it.hasNext()){
			VipConsumer vip = it.next();
			if(vip.getTelephone()==tel)
				return vip;
		}
		return null;
	}
	
	public boolean addVipConsumer(String name,String id,boolean sex,int telephone,Date date){
		return vipList.add(new VipConsumer(name, id, sex,telephone,date));
	}
	
	public boolean deleteVipConsumer(String id){
		return vipList.delete(id);
	}
	public int getAmountOfVip(){
		return vipList.vipList.size();
	}
	//--------------------------------------------------------------------促销
	public Iterator<Promotion> getPromotionList(){
		return promotionList.promotionList.iterator();
	}

	public boolean addTotalGiftPromotion(String id,double min,double max,String giftid,Date begin,Date over){
		Gift g = this.searchGiftById(giftid);
		if(g==null)return false;
		TotalGiftPromotion p = new TotalGiftPromotion(id,min, max,g,begin,over );
		return promotionList.promotionList.add(p);
	};
	
	
	public boolean addGoodsGiftPromotion(String id,String goodsId,String giftId,Date begin,Date over){
		Goods gd = this.searchGoodsById(goodsId);
		Gift gf = this.searchGiftById(giftId);
		if(gd==null||gf==null)return false;
		GoodsGiftPromotion p = new GoodsGiftPromotion(id,gd, gf,begin,over);
		return promotionList.promotionList.add(p);
	};
	public boolean addTotalDiscountPromotion(String id,double min,double max,double discount,Date begin,Date over){
		return promotionList.promotionList.add(new TotalDiscountPromotion(id,min,max,discount,begin,over));
	};
	public boolean addGoodsDiscountPromotion(String id,String goodsId,double discount,Date begin,Date over){
		Goods gd = this.searchGoodsById(goodsId);
		if(gd==null)return false;
		GoodsDiscountPromotion p = new GoodsDiscountPromotion(id,gd,discount,begin,over);
		return promotionList.promotionList.add(p);
	};
	
	public Iterator<GiftPromotion> getGiftPromotionByGoods(String goodsId){
		Goods g = searchGoodsById(goodsId);
		if(g==null)return null;
		return promotionList.getGiftPromotion(g).iterator();
	}
	public Iterator<GiftPromotion> getGiftPromotionByTotal(double total){
		return promotionList.getGiftPromotion(total).iterator();
	}
	
	public Iterator<DiscountPromotion> getDiscountPromotionByTotal(double total){
		return promotionList.getDiscountPromotion(total).iterator();
	}
	
	public Iterator<DiscountPromotion> getDiscountPromotionByGoods(String goodsId){
		Goods g = searchGoodsById(goodsId);
		if(g==null)return null;
		return promotionList.getDiscountPromotion(g).iterator();
	}
	public boolean deletePromotion(String id){
		return this.promotionList.promotionList.remove(searchPromotionByID(id));
	}
	public Promotion searchPromotionByID(String id){
		for(Promotion p:this.promotionList.promotionList){
			if(p.id.equals(id))
				return p;
		}
		return null;
	}
	public int[] getRegion(double money,int type){
		double min=0;
		double max=10000;
		for(Promotion pro:promotionList.promotionList){
			if(pro.getType()==type){
				TotalPromotion tp=(TotalPromotion)pro;
				if(tp.getMax()>money&&tp.getMin()<money)
					return null;
				else{
					if(tp.getMax()<money&&tp.getMax()>min)
						min=tp.getMax();
					else if(tp.getMin()>money&&tp.getMin()<max)
						max=tp.getMin();
				}
			}
		}
		int[] result={(int)min,(int)max};
		return result;
	}
	//------------------------------------------------------------------------用户
	
	public Iterator<User> getUserList(){
		return userList.getIterator();
	}
	
	public User searchUserById(String id){
		return userList.search(id);
	}
	
	public boolean addUser(String n,String p ,String i,int t,int a){
		return userList.add(new User(n, p, i, t,a));
	}
	
	public boolean deleteUser(String id){
		return userList.delete(id);
	}
	
	public int getAmountOfUser(){
		return userList.userList.size();
	}//-------------------------------------------------------------------------------
//------------------------------------------------------------	赠品
	public Iterator<Gift> getGiftList(){
		return giftList.giftList.iterator();
	}
	
	public Gift searchGiftById(String id){
		return giftList.search(id);
	}
	
	public boolean addGift(String name,String id){
		return giftList.add(name, id);
	}
	
	public boolean deleteGift(String id){
		return giftList.delete(id);
	}
	//-------------------------------------------------------------------------会员变更
	public void requestAddVip(String name,String id,boolean sex,int telephone,Date date){
		vipUpdataList.requestVipAdde(name, id, sex, telephone, date);
	}
	public void requsetDeleteVip(String vipId){
		vipUpdataList.requestVipDelete(vipId);
	}
	public void requsetChangerVipPoint(String vipId,int sumPointChanged,int restPointChang){
		vipUpdataList.requestVipModify(vipId, sumPointChanged,restPointChang);
	}
	public void requestChangeVip(String name,String id,boolean sex,int telephone,Date date){
		vipUpdataList.requestVipChange(name, id, sex, telephone, date);
	}
	public Iterator<VipConsumerUpdata> getVipUpdata(){
		return vipUpdataList.uplist.iterator();
	}
	//----------------------------------------------------------------------销售记录
	
	public Iterator<Bill> getSaleRecord(){
		return saleRecord.billList.iterator();
	}
	public Bill createNewBill(String id,VipConsumer vip,User cashier){
		return saleRecord.createNewBill(id, vip, cashier);
	}
	public void sellGoods(Goods goods,int sum,double discount){//改名字，防止重复
		saleRecord.currentBill.addGoods(goods, sum, discount);
	}
	public void addGift(Gift gift){
		saleRecord.currentBill.addGift(gift);
	}
	
	public void addPromotion(Promotion promotion){
		saleRecord.currentBill.addPromotion(promotion);
	}
	public String printBill(){
		return saleRecord.currentBill.print();
	}
	public Bill searchBill(String id){
		return saleRecord.searchBill(id);
	}
	public String getShopId() {
		return shopId;
	}
	public void setShopId(String shopId) {
		this.shopId = shopId;
	}
	
	
	//...........................................................................总店专用
	public void replace(Database db){
		this.vipList=db.vipList;
		this.promotionList=db.promotionList;
		String id=db.getShopId();
		Iterator<User> iu=db.getUserList();
		UserList ul=new UserList(); 
		while(iu.hasNext()){
			User user=iu.next();
			if(id.equals(user.getAdress()))
				ul.add(user);
		}
		this.userList=ul;
	}
	public void addBill(Bill bill){
		synchronized(saleRecord.billList){//网络线程
		this.saleRecord.billList.add(bill);
		}
	}
	public Database copyDB(){
		SaleRecord sr=new SaleRecord();
		VipUpdataList vu=new VipUpdataList();
		Database db=new Database(inventory,vipList,promotionList,userList,vu,giftList,sr);
		return db;
	}
	public boolean addUser(User user){
		return this.userList.add(user);
	}
}